package com.shelpe.services.sinbad.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.shelpe.services.sinbad.dao.RptCustomerMapper;
import com.shelpe.services.sinbad.model.RptCustomer;
import com.shelpe.services.sinbad.model.RptCustomerExample;
import com.shelpe.services.sinbad.model.RptCustomerExample.Criteria;
import com.shelpe.services.sinbad.model.RptCustomerParam;
import com.shelpe.services.sinbad.service.RptCustomerService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RptCustomerServiceImpl implements RptCustomerService {

	@Autowired
	RptCustomerMapper rptCustomerMapper;

	@Override
	public RptCustomer selectSums(RptCustomerParam param) {
		if (log.isDebugEnabled()) {
			log.debug("selectSums param:{}", JSON.toJSONString(param));
		}
		return rptCustomerMapper.selectSumsByExample(toExample(param));
	}

	@Override
	public List<RptCustomer> selectStat(RptCustomerParam param) {
		if (log.isDebugEnabled()) {
			log.debug("selectStat param:{}", JSON.toJSONString(param));
		}
		return rptCustomerMapper.selectStatByExample(toExample(param));
	}

	private RptCustomerExample toExample(RptCustomerParam param) {
		RptCustomerExample example = new RptCustomerExample();
		Criteria criteria = example.createCriteria();
		if (param.getStartDate() == null) {
			param.setDate(new Date());
		}
		if (param.getEndDate() == null) {
			param.setDate(new Date());
		}
		criteria.andDateBetween(param.getStartDate(), param.getEndDate());
		switch (param.getDevice()) {
		case 0:// all
			criteria.andSourceBetween((byte) 1, (byte) 5);
			break;
		case 1:// mobile
			criteria.andSourceBetween((byte) 4, (byte) 5);
			break;
		case 2:// pc
			criteria.andSourceBetween((byte) 1, (byte) 2);
			break;
		default:
			break;
		}
		criteria.andTaobaoUserIdEqualTo(param.getTaobaoUserId());
		if (log.isDebugEnabled()) {
			log.debug("toExample example:{}", JSON.toJSONString(example));
		}
		return example;
	}
}
